# @Version        : 1.0
# @Update Time    : 2025/3/15 13:16
# @File           : reset_password_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
import flet as ft


class ResetPasswordView(ft.Container):
    def __init__(self, page: ft.Page, params: dict = None):
        super().__init__()
        self.page = page
        self.params = params
        self.primary_color = "#4CAF50"  # 主绿色
        self.input_border = "#BDBDBD"  # 输入框边框色
        self.error_color = "#D32F2F"  # 错误提示色

        # 控件定义
        self.current_password_field = self._build_password_field("当前密码")  # 新增: 当前密码字段
        self.new_password_field = self._build_password_field("新密码")
        self.confirm_password_field = self._build_password_field("确认密码")
        self.submit_button = self._build_submit_button()

        # 主容器配置
        self.width = 400
        self.padding = 30

        # 设置水平垂直居中
        self.alignment = ft.alignment.center
        self.content = ft.Column(
            controls=[
                self._build_title(),  # 使用 _build_title 方法
                self._build_description(),
                self.current_password_field,  # 新增: 当前密码字段
                self.new_password_field,
                self.confirm_password_field,
                self.submit_button
            ],
            spacing=20,
            horizontal_alignment=ft.CrossAxisAlignment.CENTER,
        )

    def _build_password_field(self, label):
        field = ft.TextField(
            label=label,
            password=True,
            can_reveal_password=True,
            border_color=self.input_border,
            focused_border_color=self.primary_color,
            border_radius=8,
            height=50,
            text_size=14,
            content_padding=5,
            label_style=ft.TextStyle(color="#666666"),
            suffix_style=ft.TextStyle(color="#666666")  # 修改为 TextStyle
        )
        return field

    def _build_submit_button(self):
        return ft.ElevatedButton(
            content=ft.Text(
                "确认修改",
                size=16,
                weight=ft.FontWeight.W_500
            ),
            bgcolor=self.primary_color,
            color=ft.colors.WHITE,
            width=300,
            height=45,
            elevation=0,
            on_click=self._handle_submit,
            style=ft.ButtonStyle(
                shape=ft.RoundedRectangleBorder(radius=8),
            )
        )

    def _build_title(self):  # 新增: _build_title 方法
        return ft.Text(
            "重置密码",
            size=28,
            weight=ft.FontWeight.BOLD,
            color=self.primary_color
        )

    def _build_description(self):  # 新增: _build_description 方法
        return ft.Text(
            "请输入您的当前密码和新密码。",
            size=14,
            color="#666666",
            text_align=ft.TextAlign.CENTER
        )

    def _handle_submit(self, e):
        if not self.current_password_field.value:  # 新增: 检查当前密码是否为空
            self.current_password_field.error_text = "请输入当前密码"
            self.current_password_field.update()
            return
        if not self.new_password_field.value or not self.confirm_password_field.value:
            self.new_password_field.error_text = "请输入新密码"
            self.confirm_password_field.error_text = "请输入确认密码"
            self.new_password_field.update()
            self.confirm_password_field.update()
            return
        # 输入验证
        if self.new_password_field.value != self.confirm_password_field.value:
            self.confirm_password_field.error_text = "两次输入的密码不一致"
            self.confirm_password_field.update()
            return

        # 清除错误状态
        self.current_password_field.error_text = None  # 新增: 清除当前密码错误状态
        self.confirm_password_field.error_text = None
        self.current_password_field.update()
        self.confirm_password_field.update()

        # 触发回调
        info = {
            "current_password": self.current_password_field.value,  # 新增: 传递当前密码
            "new_password": self.new_password_field.value,
            "confirm_password": self.confirm_password_field.value
        }
        self._show_success_snackbar(info)

    def _show_success_snackbar(self, info: dict):
        self.page.show_snack_bar(
            ft.SnackBar(
                content=ft.Text("修改成功"),
                open=True,
                duration=2000
            )
        )
        print(info)
        self.page.go("/login")
